﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;
using TRPOP.CMS.Message;
using TRPOP.CMS.Message.model;
using TRPOP.CMS.Model;
namespace TRPOP.CMS.Web.cms.Message
{
    public partial class message_list : BaseAdmin
    {
        protected IMessage m_IMessage = MessageAccess.GetInstance();
        string strCondation = " AND bIsDelete='false'";

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindType();
                BindStatus();
                BindData();
            }
        }

        /// <summary>
        /// 删除操作（软删除 bIsDelete=True）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            LinkButton lbtn = (LinkButton)sender;
            int intID = 0;
            int.TryParse(lbtn.CommandArgument, out intID);
            m_IMessage.DeleteMessageInfo(intID);

            SearchCondition();
            BindData();
            Pager.Page_ReLoad();
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Search_Click(object sender, EventArgs e)
        {
            SearchCondition();
            Pager.Page_ReLoad();
            BindData();
        }
        #endregion

        #region 页面方法
        /// <summary>
        /// 绑定留言信息
        /// </summary>
        protected void BindData()
        {
            string strOrderBy = " dtCreationTime DESC";
            int m_intRecordCount = 0;

            if (Pager.PageSize <= 0) { Pager.PageSize = SYSConfig.SystemVar_AdminContentPageNum; }

            rptList.DataSource = m_IMessage.GetMessage_MessageReplyInfoListByPager(Pager.CurrentPage, Pager.PageSize, strCondation, strOrderBy, out m_intRecordCount);
            Pager.ShowRecordCount = true;
            Pager.ShowPageSize = true;
            Pager.ShowCurrentPage = true;
            Pager.RecordCount = m_intRecordCount;
            rptList.DataBind();
        }

        /// <summary>
        /// 绑定留言类型
        /// </summary>
        protected void BindType()
        {
            ddlType.DataSource = m_IMessage.GetMessageTypeInfoList(0, "", "");
            ddlType.DataTextField = "nvcMessageTypeName";
            ddlType.DataValueField = "iMessageTypeID";
            ddlType.DataBind();
            ddlType.Items.Insert(0, new ListItem("全部", "0"));
        }

        /// <summary>
        /// 绑定留言状态
        /// </summary>
        protected void BindStatus()
        {
            ddlStatus.DataSource = m_IMessage.GetMessageStatusList(0, "", "");
            ddlStatus.DataTextField = "nvcMessageStatusName";
            ddlStatus.DataValueField = "iMessageStatusID";
            ddlStatus.DataBind();
            ddlStatus.Items.Insert(0, new ListItem("全部", "0"));
        }

        /// <summary>
        /// 搜索条件
        /// </summary>
        protected void SearchCondition()
        {
            if (!String.IsNullOrEmpty(txtMessageTitle.Text))
            {
                strCondation += "  AND ( nvcMessageTitle LIKE '%" + Server.HtmlEncode(txtMessageTitle.Text.Trim()).SQLFilter() + "%'  " +
                                "         OR nvcMessageSend  LIKE '%" + Server.HtmlEncode(txtMessageTitle.Text.Trim()).SQLFilter() + "%' ) ";
            }
            if (ddlType.SelectedValue.ToInt() > 0)
            {
                strCondation += " AND iMessageTypeID=" + ddlType.SelectedValue.ToInt();
            }
            if (ddlStatus.SelectedIndex > 0)
            {
                strCondation += " AND iMessageStatusID=" + ddlStatus.SelectedValue.ToInt();
            }
        }
        #endregion
    }
}